package com.xisoft.devise.controller;


import com.xisoft.base.page.PageResult;
import com.xisoft.base.page.Query;
import com.xisoft.common.aspect.AppMonitorRuning;
import com.xisoft.common.pubEnv.PropertiesUtil;
import com.xisoft.common.result.Result;
import com.xisoft.devise.entity.TableEntity;
import com.xisoft.devise.service.IGenService;
import com.xisoft.generator.utils.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author xzh
 * @Date 2021/8/16
 * @description controller请求
 * 注意：本内容仅限于上海熙软科技有限公司内部，传阅，禁止外泄以及用于其他商业目的
 */
@RestController
@RequestMapping ("/gen")
public class GenController
{
    @Qualifier ("GenServiceImpl")
    @Autowired
    IGenService service;
    
    @RequestMapping ("/table/genCodeBytable")
    @AppMonitorRuning (method = "GenController.genCodeBytable", funEffect = "根据table生成代码使用！")
    public Result genCodeBytable(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return Result.ok(service.genCodeBytable(request.getParameter("table"))); }
    
    @RequestMapping ("/table/makeCode")
    @AppMonitorRuning (method = "GenController.makeCode", funEffect = "根据json生成代码使用！")
    public Result makeCode(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        ResultEntity result = service.makeCode(request.getParameter("tableJson"));

        return Result.ok(result);
    }

    @RequestMapping ("/table/create")
    @AppMonitorRuning (method = "GenController.createCode", funEffect = "直接生成代码使用！")
    public Result createCode(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        String filePath = this.getClass().getResource("/config/gentemplate.properties").getPath();
        Map< String,String > proMap = PropertiesUtil.getAllProperties(filePath);
        ResultEntity result = service.createCode(request.getParameter("tableJson"));
        // byte[] bytes = FileUtilbc.getFileZipByte(proMap.get("backendPath") + "/src");
        // response.reset();
        // response.setHeader("Content-Disposition", "attachment; filename=\"code.zip\"");
        // response.addHeader("Content-Length", "" + bytes.length);
        // response.setContentType("application/octet-stream; charset=UTF-8");
        // // response.setContentType("application/zip; charset=UTF-8");
        // response.getOutputStream().write(bytes);

        return Result.ok(result);
    }


    @GetMapping ("/table/genCode")
    @AppMonitorRuning (method = "GenController.genCode", funEffect = "行操作生成代码！")
    public Result genCode(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return Result.ok(service.genCode(request.getParameter("table"))); }

    @GetMapping ("/table/field")
    @AppMonitorRuning (method = "GenController.getTablefield", funEffect = "table/field！")
    public Result getTablefield(HttpServletRequest request, HttpServletResponse response) throws Exception
    { return Result.ok(service.getFieldList(request.getParameter("table"))); }

    /************************************************************* <br>
     *
     *说明: <br>
     *
     * @Description   <br>
     * @Date Created on 2022/8/10  15:21  <br>
     * @param query <br>
     * @return com.xisoft.common.result.Result<PageResult < TableEntity>>  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    @ResponseBody
    @RequestMapping ("/table/page")
    @AppMonitorRuning (method = "GenController.pageTable", funEffect = "table/page！")
    @GetMapping ("table/page")
    public Result< PageResult< TableEntity > > pageTable(Query query) throws Exception
    {
        PageResult< TableEntity > page = service.page(query);

        return Result.ok(page);
    }

}